package com.hugothauan.cowboyshoot;

import java.util.ArrayList;
import java.util.List;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.hugothauan.cowboyshoot.*;
import com.game.ThauanFramework.BitmapStorage;
import com.game.ThauanFramework.GameObject;


public class MenuView extends View implements Runnable{

	private String TAG = "Menu View";
	
	private Thread mainThread;
	private int interval = 10;
	private boolean running = true;
	
	private List<GameObject> gameObjects = new ArrayList<GameObject>();
	
	private Rect tittleRect;
	private Rect playButtonRect;
	private Rect creditsButtonRect;
	
	private ImageButton playButton;
	private ImageButton creditsButton;
	
	BluetoothAdapter btAdapter;
	
	public MenuView(Context context, BluetoothAdapter btAdapter) 
	{
		super(context);	
		running = true;
		this.btAdapter = btAdapter;
		this.setFocusable(true);
		
		Start();	
	}
	
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh)
	{
		if(MinhasCoisas.getCanvasHeight() == 0){
		MinhasCoisas.setCanvasHeight(h);
		MinhasCoisas.setCanvasWidth(w);
		}
		
		Log.i(TAG, String.valueOf(h));
		Log.i(TAG, String.valueOf(w));
		Log.i(TAG, String.valueOf(h/100));
		Log.i(TAG, String.valueOf(w/100));
	
		
		this.setBackgroundResource(R.drawable.topo_menu);
		
		int Xoffset = (MinhasCoisas.getCanvasWidth() - BitmapStorage.getInstance().getPlayButton().getWidth())/2;
		int YoffSet = (int) (MinhasCoisas.getHeightPercent(true)*60);
		
		Log.i(TAG, String.valueOf(YoffSet));
		
		this.playButtonRect = new Rect(Xoffset, YoffSet,BitmapStorage.getInstance().getPlayButton().getWidth() + Xoffset, BitmapStorage.getInstance().getPlayButton().getHeight() + YoffSet);		
		playButton = new ImageButton(BitmapStorage.getInstance().getPlayButton(), playButtonRect, gameObjects);
		//creditsButton = new ImageButton(BitmapStorage.getInstance().getMonstro_03(), creditsButtonRect, gameObjects);
		
		mainThread = new Thread(this);
		mainThread.start();
		
		Log.i(TAG, "on size Changed");
	}
	
	public boolean onTouchEvent(MotionEvent event) 
	{
		switch (event.getAction()) 
		{
			case MotionEvent.ACTION_DOWN:
				int x = (int)event.getRawX();
				int y = (int)event.getRawY();
				
				if(this.playButton.onTap(x, y))
				{
					HandleBluetoothAdapter(btAdapter);
				}	
				break;
		}
		return super.onTouchEvent(event);
	}
	private void HandleBluetoothAdapter(BluetoothAdapter btAdapter)
	{
		try{
			if(btAdapter != null)
			{
				//MinhasCoisas.Show("You can play!\nThis device can make bluetooth connections");
				
				if (!btAdapter.isEnabled()) {
					
				    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
				    MinhasCoisas.getCurrentActivity().startActivityForResult(enableBtIntent, REQUEST.REQUEST_ENABLE_BT);
				}else
				{
					// Change Activity for Select Device Activity
					Intent intent = new Intent(MinhasCoisas.getCurrentActivity(), DeviceList.class);
					MinhasCoisas.getCurrentActivity().startActivity(intent);
				}	
			}else
			{
				btAdapter = BluetoothAdapter.getDefaultAdapter();
				HandleBluetoothAdapter(btAdapter);
			}
			
		}catch(Exception e)
		{
			MinhasCoisas.Show("Error: Cannot turn bluetooth on. Please, try again");
		}	
	}
	
	//------------------------------------------------------------- START
	public void Start()
	{
		
	}
	//------------------------------------------------------------- UPDATE
	public void Update()
	{
	
	}
	//------------------------------------------------------------- DRAW
	@Override
	public void draw(Canvas canvas) 
	{
		super.draw(canvas);
		for(GameObject go : gameObjects)
		{
			go.Draw(canvas);
		}
		
	}
	
	public void SetAlive(boolean bool)
	{
		this.running = bool;
	}
	
	@Override
	public void run() {
		
		// Starts the loop
		while(running)
		{
			try
			{
				Thread.sleep(interval);
			}
			catch(InterruptedException e)
			{
				running = false;
				Log.i(TAG, "Erro no processo de Thread", e);
			}
			
			Update();
			postInvalidate();			
		}

	}
}
